import requests
from urllib.parse import urlencode


class MiddlewareAPI:

	def __init__(self):
		# 火星来客接口
		self.baseUri = 'http://www.mapprouter.com/api'
		self.pid = 'mm_53723466_40674152_160500778'
		# 淘口令接口
		self.tao_password_url = 'http://api.kfsoft.net/api/tb/tkl/v1.php'
		self.tao_user_key = 'vpEWEvv06gwXFjXe'
	# 首页 获取 关键字 对应产品
	def get_index_goods_page(self, keyword, pageNum, pageSize):
		params = {'pid': self.pid, 'q': keyword, 'pageNum': pageNum, 'pageSize': pageSize}
		r = requests.get(self.baseUri + '/search/coupon_item_list', params=params)
		return r.json()
		
	# 优惠劵详情
	def get_cunpon_details(self, ulandUrl):
		params = {'pid': self.pid, 'ulandUrl': ulandUrl}
		r = requests.get(self.baseUri + '/coupon_item/detail', params=params)
		return r.json()

	# 淘口令生成
	def get_tao_password(self, uland):
		params = {'user_key': self.tao_user_key, 'url': 'https:' + uland}
		r = requests.get(self.tao_password_url, params=params)
		return r.json()

	# 商品详情（这是一个全部商品详情的接口，因为这里数据没有对接好，所以暂时没有使用）
	def get_goods_detail(self, goods_id):
		r = requests.get('https://acs.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?data={%22itemNumId%22%3A%22' + goods_id + '%22}')		
		return r.json()

	# 商品详情图片
	def get_goods_details_images(self, url):
		r = requests.get('http:' + url)
		return r.json()
	
	# 获取搜索联想的结果
	def get_search_auto_result(self, query):
		r = requests.get('https://suggest.taobao.com/sug?code=utf-8&q=' + query)
		return r.json()